﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AntikCompta.Data;

namespace AntikCompta
{
    public partial class Synthese : Form
    {
        private Exercice mExercice;
        private DataMng mDataMng;
        internal Synthese(ref DataMng aDataMng, ref Exercice aExercice)
        {
            mExercice = aExercice;
            mDataMng = aDataMng;
            InitializeComponent();

            Text += ": " + mExercice.Annee;

            mExercice.DataChanged += new DataChangedHandler(mExercice_DataChanged);

            RefreshView();
        }

        void mExercice_DataChanged()
        {
            RefreshView();
        }

        private void RefreshView()
        {
            List<Produit> liste;
            if (mDataMng.GetBilan(mExercice.Annee, out liste) == false)
                return;

            float tot_achats = 0;
            float tot_achats_partic = 0;
            float tot_achats_march = 0;

            float tva = 0;
            float tot_ventes = 0;
            float tot_ventes_partic = 0;
            float tot_ventes_march = 0;
            float tot_ventes_eu = 0;
            float tot_ventes_heu = 0;
            float tot_benef = 0;

            foreach(Produit p in liste)
            {
                if (p.DateAchat.Year == mExercice.Annee)
                {
                    float prixAchat = p.Vendeur.GetPrixAchat(p.PrixAchat);
                    tot_achats += prixAchat;

                    VendeurMarchandHt v = p.Vendeur as VendeurMarchandHt;
                    if (v == null)
                        tot_achats_partic += prixAchat;
                    else
                        tot_achats_march += prixAchat;
                }

                float prixVente = p.CalculPrixVente(mExercice.Annee);
                tot_ventes += prixVente;

                AcheteurParticulier ap = p.Acheteur as AcheteurParticulier;
                if (ap != null)
                    tot_ventes_partic += prixVente;
                else
                {
                    AcheteurMarchandFr am = p.Acheteur as AcheteurMarchandFr;
                    if (am != null)
                        tot_ventes_march += prixVente;
                    else
                    {
                        AcheteurExportEu ae = p.Acheteur as AcheteurExportEu;
                        if (ae != null)
                            tot_ventes_eu += prixVente;
                        else
                            tot_ventes_heu += prixVente;
                    }
                }

                tva += p.CalculTva(mExercice.Annee);
                tot_benef += p.CalculBenefice(mExercice.Annee);
            }

            lbl_tot_achats.Text = Math.Round(tot_achats, 2) +" €";
            lbl_tot_achats_partic.Text = Math.Round(tot_achats_partic, 2) + " €";
            lbl_tot_achats_march.Text = Math.Round(tot_achats_march, 2) + " €";

            lbl_tva.Text = Math.Round(tva, 2) + " €";
            lbl_tot_ventes.Text = Math.Round(tot_ventes, 2) + " €";
            lbl_tot_ventes_partic.Text = Math.Round(tot_ventes_partic, 2) + " €";
            lbl_tot_ventes_march.Text = Math.Round(tot_ventes_march, 2) + " €";
            lbl_tot_ventes_eu.Text = Math.Round(tot_ventes_eu, 2) + " €";
            lbl_tot_ventes_heu.Text = Math.Round(tot_ventes_heu, 2) + " €";
            lbl_benef.Text = Math.Round(tot_benef, 2) + " €";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Synthese_FormClosing(object sender, FormClosingEventArgs e)
        {
            mExercice.DataChanged -= new DataChangedHandler(mExercice_DataChanged);
        }
    }
}
